package io;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class IOTest02 {
    public static void main(String[] args) throws IOException {
/*        //单字节写:
        FileOutputStream fos2 = new FileOutputStream("fos2.dat");
        fos2.write(10);
        fos2.write(2);
        System.out.println("输出完毕");
        fos2.close();

        //单字节读:
        FileInputStream fis2 = new FileInputStream("fos2.dat");
        int d = fis2.read();
        System.out.println(d);
        d = fis2.read();
        System.out.println(d);
        d = fis2.read();
        System.out.println(d);
        fis2.close();*/

/*        //单字节文件复制：慢
        FileInputStream fis2 = new FileInputStream("111.jpg");
        FileOutputStream fos2 = new FileOutputStream("");
        long start = System.currentTimeMillis();
        int d;
        while ((d=fis2.read())!=-1){
            fos2.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕！耗时:"+(end-start)+"ms");
        fis2.close();
        fos2.close();*/

/*        //块读写的文件复制操作：快
        FileInputStream fis2 = new FileInputStream("111.jpg");
        FileOutputStream fos2 = new FileOutputStream("");
        long start = System.currentTimeMillis();
        byte[] data = new byte[1024*10];
        int len;
        while ((len=fis2.read())!=-1){
            fos2.write(data,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕！耗时:"+(end-start)+"ms");
        fis2.close();
        fos2.close();*/

/*        //写文本数据:
        FileOutputStream fos2 = new FileOutputStream("fos2.txt");
        fos2.write("IO流的第二次练习".getBytes(StandardCharsets.UTF_8));
        //String name = ",写文本数据的另一种写法";
        //byte[] data = name.getBytes(StandardCharsets.UTF_8);
        //fos2.write(data);
        System.out.println("写入完毕");
        fos2.close();*/

        //读取文本数据:
        File file = new File("fos2.txt");
        FileInputStream fis2 = new FileInputStream(file);
        byte[] data = new byte[(int)file.length()];
        fis2.read(data);
        String line = new String(data, StandardCharsets.UTF_8);
        System.out.println(line);
        fis2.close();

    }
}
